package neo.tetris.rpc.session;

import neo.sourceEngine.network.rpc.session.RpcSession;
import neo.tetris.bean.model.Hall;
import neo.tetris.bean.model.World;
import neo.tetris.bean.model.battle.BattlePlayerHuman;
import neo.tetris.bean.persist.UserInstance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author feng.lee
 * @create on 2013-12-6
 */
public class SessionModel extends RpcSession {
	static Logger logger = LoggerFactory.getLogger(SessionModel.class);

	final private World world;

	private UserInstance logined;

	public UserInstance getLogined() {
		return logined;
	}

	public void setLogined(UserInstance value) {
		logined = value;
	}

	private Hall enteredHall;

	public Hall getEnteredHall() {
		return enteredHall;
	}

	public void setEnteredHall(Hall value) {
		this.enteredHall = value;
	}

	private BattlePlayerHuman player;

	public BattlePlayerHuman getPlayer() {
		return player;
	}

	public void setPlayer(BattlePlayerHuman value) {
		this.player = value;
	}

	public SessionModel() {
		super();

		this.world = World.instance;
		this.world.connectedSessions.add(this);
	}

	public void close() {
		this.world.connectedSessions.remove(this);
		if (this.logined != null) {
			this.logined.session = null;
			this.world.loginedSessions.remove(this.logined.uuid);
			if (this.enteredHall != null)
				this.enteredHall.observer.deleteObserver(this);
			/**
			if (this.player != null) {
				Battle battle = player.battle;
				world.battles.remove(battle.instanceUuid);
				// clean all players
				for (int i = 0; i < battle.players.length; i++) {
					BattlePlayer member = this.player.battle.players[i];
					if (member instanceof BattlePlayerHuman) {
						BattlePlayerHuman human = (BattlePlayerHuman) member;
						human.session.player = null;
					}
				}
			}
			**/
		}
	}
}
